<template>
  <div class="app-container">
    <el-table :data="taskGroup" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table :data="props.row.projectList" style="backgr" :summary-method="getSummaries" show-summary border
            :row-class-name="tableRowClassName">
            <el-table-column label="编号" type="index" align="center" width="100"></el-table-column>
            <el-table-column label="专利名称" prop="proName" align="center"></el-table-column>

            <el-table-column label="评分专家" prop="userName" align="center"></el-table-column>
            <el-table-column label="评审时间" prop="updateTime" align="center" :formatter="dateFormat"></el-table-column>
            <el-table-column label="评审分数" prop="score" align="center" sortable>
              <template slot-scope="scope">
                <el-tag type="primary" v-if="scope.row.score==null">暂无评审~</el-tag>
                <el-tag type="danger" v-else>{{scope.row.score}}</el-tag>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="专利所在小组" prop="groupId" align="center">
      </el-table-column>
      <el-table-column label="小组名称" prop="groupName" align="center">
      </el-table-column>
      <el-table-column label="专利名称" prop="proName" align="center">
      </el-table-column>
      <el-table-column label="上传时间" prop="proCreatetime" :formatter="dateFormat" align="center">
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="taskGroup" />
  </div>
</template>
<script>
import { selectMyScore } from "@/api/system/project/project";
import moment from "moment";

export default {
  data () {
    return {
      myScoreList: [],
      taskGroup: [],
      total: 0,
      // 查询参数
      queryParams: {
        groupName: null,
        proAuthor: null,
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      selectMyScore().then(res => {
        this.myScoreList = res.rows
        let dataInfo = [];
        this.myScoreList.forEach((item, index) => {
          let { id, proName, groupId, proType, groupName, proCreatetime } = item;
          if (!dataInfo[groupId]) {
            dataInfo[groupId] = {
              id,
              proCreatetime,
              groupId,
              proName,
              groupName,
              proType,
              projectList: []
            }
          }
          dataInfo[groupId].projectList.push(item);
        });
        this.taskGroup = Object.values(dataInfo);
        for (var i = 0; i < this.taskGroup.length; i++) {
          for (var j = 0; j < this.taskGroup[i].projectList.length; j++) {
            if (this.taskGroup[i].projectList[j].score == '') {
              console.log(this.taskGroup[i].projectList[j].score)
            }
          }
        }
      })
    },
    dateFormat: function (row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm");
    },
    tableRowClassName ({ row, rowIndex }) {
      if (rowIndex % 2 == 0) {
        return 'warning-row';
      } else if (rowIndex % 2 != 0) {
        return 'success-row';
      }
      return '';
    },
    getSummaries (param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '当前平均分';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += ' 分';
        } else {
          sums[index] = '';
        }
      });

      return sums;
    }

  }
}
</script>
<style>
.el-table .warning-row {
  background: rgba(193, 213, 255, 0.849);
}

.el-table .success-row {
  background: #e8f8dfb0;
}
</style>